package com.css.fxfzaqbz.modules.data.controller;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.modules.data.entity.Expert;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.entity.Report;
import com.css.fxfzaqbz.modules.data.queryParams.ActMap;
import com.css.fxfzaqbz.modules.data.queryParams.ActReport;
import com.css.fxfzaqbz.modules.data.queryParams.ImportantActivityQueryParams;
import com.css.fxfzaqbz.modules.data.service.ExpertService;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.modules.data.service.ReportService;
import com.css.fxfzaqbz.modules.data.vo.ExpertVo;
import com.css.fxfzaqbz.modules.data.vo.HistoryReportVo;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.service.SysAreaService;
import com.css.fxfzaqbz.zcpt.sys.service.impl.SUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 重大活动安保管理子模块 -报告管理
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class IAReportController {

    @Resource
    ReportService reportService;
    @Resource
    AttachmentInfoService attachmentInfoService;
    @Resource
    ExpertService expertService;


    /**
     * 新增Report
     *
     * @param report
     * @return
     */
    @PostMapping("/saveReport")
    public RestResponse saveReport(@RequestBody Report report) {
        RestResponse restResponse = null;

        try {
            String userID = PlatformSessionContext.getUserID();
            report.setSrId(userID);
            restResponse = RestResponse.succeed(reportService.saveReport(report));
        } catch (Exception e) {
            log.error("新增报告管理失败", e);
            restResponse = RestResponse.fail("新增报告管理失败!");
        }
        return restResponse;
    }


    /**
     * 根据 活动id 查询当前活动的报告列表
     *
     * @param id 重大活动id信息
     * @return 报告列表
     */
    @GetMapping("/getAllReportById/{id}")
    public RestResponse getAllReportById(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(reportService.getAllReportById(id));
        } catch (Exception e) {
            log.error("查询报告信息失败!", e);
            restResponse = RestResponse.fail("查询报告信息失败");
        }
        return restResponse;
    }

    /**
     * 根据 活动id 查询当前活动的系统报告
     *
     * @param id 重大活动id信息
     * @return 报告列表
     */
    @GetMapping("/getReportByIaId")
    public RestResponse getReportByIaId(@RequestParam("id") String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(reportService.getReportByIaId(id));
        } catch (Exception e) {
            log.error("查询报告信息失败!", e);
            restResponse = RestResponse.fail("查询报告信息失败");
        }
        return restResponse;
    }

    /**
     * 根据 id 查询报告信息
     *
     * @param id 报告id
     * @return 报告信息
     */
    @GetMapping("/getReportById/{id}")
    public RestResponse getReportById(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(reportService.getReportById(id));
        } catch (Exception e) {
            log.error("查询报告信息失败!", e);
            restResponse = RestResponse.fail("查询报告信息失败");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     *
     * @param ids 报告id列表
     * @return
     */
    @PostMapping("/delReports/{ids}")
    public RestResponse delReports(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try {
            reportService.delReports(ids);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("删除报告失败!", e);
            restResponse = RestResponse.fail("删除报告失败!");
        }
        return restResponse;
    }


    @PostMapping("/updateReport")
    public RestResponse<Report> updateReport(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(reportService.updateReport(report));
        } catch (Exception e) {
            log.error("修改报告数据失败!", e);
            restResponse = RestResponse.fail("修改报告数据失败!");
        }
        return restResponse;
    }

    /**
     * 成果报告
     *
     * @param
     * @return
     */
    @GetMapping("/actReport")
    public RestResponse actReport(HttpServletRequest request, ActReport actReport) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            actReport.setCurPage(curPage);
            actReport.setPageSize(pageSize);
            restResponse = RestResponse.succeed(reportService.getActReport(actReport));
        } catch (Exception e) {
            log.error("查询成果报告信息失败!", e);
            restResponse = RestResponse.fail("查询成果报告信息失败");
        }
        return restResponse;
    }

    /**
     * 获取当前登陆人所在省市，以及判断当前登陆人是否属于国家级管理员
     *
     * @param
     * @return
     */
    @GetMapping("/getLoginInfo")
    public RestResponse getLoginPersonInfo() {
        RestResponse restResponse = null;
        try {
            Map<String, Object> map = reportService.getLoginPersonInfo();
            restResponse = RestResponse.succeed(map);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看图片
     */
    @GetMapping("/getPath/{id}")
    public RestResponse getPath(@PathVariable("id") String attachId) {
        RestResponse restResponse = null;
        try {
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(attachId);
            restResponse = RestResponse.succeed(attach);
        } catch (Exception e) {
            String errormessage = "查看图片失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 成果图件
     *
     * @param
     * @return
     */
    @GetMapping("/actMap")
    public RestResponse getActMap(HttpServletRequest request, ActMap actMap) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            actMap.setCurPage(curPage);
            actMap.setPageSize(pageSize);
            restResponse = RestResponse.succeed(reportService.getActMap(actMap));
        } catch (Exception e) {
            log.error("查询成果图件信息失败!", e);
            restResponse = RestResponse.fail("查询成果图件信息失败");
        }
        return restResponse;
    }

    /**
     * 获取报告id
     */
    @PostMapping("/getCode")
    public RestResponse getCode(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            String code = reportService.getCode(report);
            restResponse = RestResponse.succeed(code);
        } catch (Exception e) {
            String errormessage = "获取报告id失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    @PostMapping("/getReportByFC")
    public RestResponse getReportByFC(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            Report report1 = reportService.getReportByFC(report);
            if (report1 == null) {
                restResponse = RestResponse.succeed("0");
            } else if (!report1.getSrStatus().equals("0")) {
                restResponse = RestResponse.succeed("2");
            } else {
                restResponse = RestResponse.succeed("1");
            }

        } catch (Exception e) {
            String errormessage = "判断失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //重复上传校验
    @PostMapping("/reCheck")
    public RestResponse reCheck(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            Report report1 = reportService.getReportByFC(report);
            if (report1 == null) {
                restResponse = RestResponse.succeed("0");
            } else {
                restResponse = RestResponse.succeed("1");
            }

        } catch (Exception e) {
            String errormessage = "判断失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 历史上传版本查看
     */
    @PostMapping("/getHistory")
    public RestResponse getHistory(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            ArrayList<HistoryReportVo> historyReportVos = reportService.getHistory(report);
            restResponse = RestResponse.succeed(historyReportVos);
        } catch (Exception e) {
            String errormessage = "历史上传版本查看失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/getAllReport")
    public RestResponse getAllReport(Report report) {
        RestResponse restResponse = null;
        try {
            List<Report> reportList = reportService.getAllReport(report);

            restResponse = RestResponse.succeed(reportList);
        } catch (Exception e) {
            log.error("查询审核信息失败!", e);
            restResponse = RestResponse.fail("查询审核信息失败");
        }
        return restResponse;
    }

    @PostMapping("/updateReportStatus")
    @Transactional
    public RestResponse updateReportStatus(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {

            restResponse = RestResponse.succeed(reportService.updateReportStatus(report));

        } catch (Exception e) {
            log.error("修改审核信息失败!", e);
            restResponse = RestResponse.fail("修改审核信息失败");
        }
        return restResponse;
    }

    @PostMapping("/saveExpert")
    public RestResponse saveExpert(@RequestBody List<Expert> experts) {
        RestResponse restResponse = null;
        try {
            Report report = expertService.saveExpert(experts);
            restResponse = RestResponse.succeed(report);
        } catch (Exception e) {
            log.error("修改审核信息失败!", e);
            restResponse = RestResponse.fail("修改审核信息失败");
        }
        return restResponse;
    }

    @GetMapping("/getAllExpert")
    public RestResponse getAllExpert(Report report) {
        RestResponse restResponse = null;
        try {
            List<ExpertVo> expertVoList = reportService.getAllExpert(report);
            restResponse = RestResponse.succeed(expertVoList);
        } catch (Exception e) {
            log.error("查询专家意见信息失败!", e);
            restResponse = RestResponse.fail("查询专家意见信息失败");
        }
        return restResponse;
    }

    @GetMapping("/getAttach/{attachId}")
    public RestResponse getAttach(@PathVariable("attachId") String attachId) {
        RestResponse restResponse = null;
        try {
            AttachmentInfoEntity attach = attachmentInfoService.getAttach(attachId);
            restResponse = RestResponse.succeed(attach);
        } catch (Exception e) {
            log.error("查询附件信息失败!", e);
            restResponse = RestResponse.fail("查询附件信息失败");
        }
        return restResponse;
    }

    /**
     * 提交评审意见
     *
     * @param expert
     * @return
     */
    @PostMapping("/updateExpert")
    public RestResponse updateExpert(@RequestBody Expert expert) {
        RestResponse restResponse = null;
        try {
            expertService.updateExpert(expert);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("提交评审意见失败!", e);
            restResponse = RestResponse.fail("提交评审意见失败");
        }
        return restResponse;
    }

    /**
     * 提交审核
     *
     * @param srId
     * @return
     */
    @PostMapping("/submitSh/{srId}")
    public RestResponse submitSh(@PathVariable("srId") String srId) {
        RestResponse restResponse = null;
        try {
            reportService.submitSh(srId);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("提交审核失败!", e);
            restResponse = RestResponse.fail("提交审核失败");
        }
        return restResponse;
    }

    /**
     * 查看评审意见
     *
     * @param srId
     * @return
     */
    @GetMapping("/getAttachIds/{srId}")
    public RestResponse getAttachIds(@PathVariable("srId") String srId) {
        RestResponse restResponse = null;
        try {
            List<String> attachIds = reportService.getAttachIds(srId);
            restResponse = RestResponse.succeed(attachIds);
        } catch (Exception e) {
            log.error("查看评审意见失败!", e);
            restResponse = RestResponse.fail("查看评审意见失败");
        }
        return restResponse;
    }

    /**
     * 上传评审意见
     *
     * @param report
     * @return
     */
    @PostMapping("/uploadYiJian")
    public RestResponse uploadYiJian(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            reportService.uploadYiJian(report);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("上传评审意见失败!", e);
            restResponse = RestResponse.fail("上传评审意见失败");
        }
        return restResponse;
    }

    /**
     * 重新上传报告
     *
     * @param report
     * @return
     */
    @PostMapping("/reUpload")
    public RestResponse reUpload(@RequestBody Report report) {
        RestResponse restResponse = null;
        try {
            reportService.reUpload(report);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            log.error("重新上传报告失败!", e);
            restResponse = RestResponse.fail("重新上传报告失败");
        }
        return restResponse;
    }

}
